<!DOCTYPE html>
<html>

<head>
    <meta name="layout" content="main"/>
    <g:set var="entityName" value="${message(code: 'account.label', default: 'Account')}"/>
    <title>机构：${account.name}</title>
</head>

<body class="fixed-navbar fixed-sidebar">
<div class="small-header">
    <div class="hpanel">
        <div class="panel-body">
            <div id="hbreadcrumb" class="pull-right">
                <ol class="hbreadcrumb breadcrumb">
                    <li>贷款管理系统</li>
                    <li>
                        <g:link controller="bocAccount" action="index">机构</g:link>
                    </li>
                    <li class="active">
                        <span>信息编辑</span>
                    </li>
                </ol>
            </div>

            <h2 class="font-light m-b-xs">
                机构: ${this.account.name}
            </h2>
        </div>
    </div>
</div>

<div class="content animate-panel">
    <div class="row">
        <div class="hpanel hblue">
            <div class="panel-heading">
                机构信息编辑
            </div>

            <div class="panel-body">
                <g:form id="${this.account?.id}" action="update" method="PUT" class="form-horizontal">
                    <g:if test="${flash.message}">
                        <div class="message" role="status">${flash.message}</div>
                    </g:if>
                    <g:hasErrors bean="${this.account}">
                        <ul class="errors" role="alert">
                            <g:eachError bean="${this.account}" var="error">
                                <li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>>
                                    <g:message error="${error}"/>
                                </li>
                            </g:eachError>
                        </ul>
                    </g:hasErrors>
                    <div class="form-group">
                      <g:textField name="type.id" value="${com.next.AccountType.findByName('机构')?.id}" class="hide"/>
                        <label class="col-md-3 control-label">上级机构</label>

                        <div class="col-md-3"><g:select name="parent.id" value="${this.account?.parent?.id}"
                                                        from="${com.next.Account.findAllByType(com.next.AccountType.findByName('机构'))}" optionKey="id"
                                                        optionValue="name" class="form-control" noSelection="${['null': '请选择']}"/></div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label">机构等级</label>

                        <div class="col-md-3"><g:select name="level.id" value="${this.account?.level?.id}"
                                                        from="${com.next.AccountLevel.findAllByType(com.next.AccountLevelType.findByName('机构等级'))}" optionKey="id"
                                                        optionValue="name" class="form-control" noSelection="${['null': '请选择']}"/></div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label">机构编号</label>

                        <div class="col-md-3"><g:textField name="code" value="${this.account?.code}"
                                                           class="form-control"/></div>
                    </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label">机构简称</label>

                            <div class="col-md-3"><g:textField name="name" value="${this.account.name}"
                                                               class="form-control"/></div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label">所在省</label>

                            <div class="col-md-3"><g:select name="province.id" id="province" value="${this.account?.province?.id}"
                                                            from="${com.next.Province.list()}" optionKey="id"
                                                            optionValue="name" class="form-control" noSelection="${['null': '请选择']}"/></div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label">所在市</label>

                            <div class="col-md-3">
                              <select id="city" name="city.id" value="${this.account?.city?.id}" class="form-control cityName">
                            </select>
                            <%-- <g:select name="city.id" value="${this.account?.city?.id}"
                                                            from="${com.next.City.list()}" optionKey="id"
                                                            optionValue="name" class="form-control" noSelection="${['null': '请选择']}"/> --%>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label">地址</label>

                            <div class="col-md-3"><g:textField name="officeAddress" value="${this.account.officeAddress}"
                                                               class="form-control"/></div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label">状态</label>
                            <div class="col-md-3 checkbox-inline">
                                <g:radioGroup class="checkbox-inline" name="active" value="${this.account?.active}" labels="['启用','禁用']" values="[true,false]">
                                    ${it.radio} ${it.label}
                                </g:radioGroup>
                            </div>
                        </div>

                        <input type="hidden" id="provinceId" value="${this.account?.province?.id}">
                        <input type="hidden" id="cityId" value="${this.account?.city?.id}">

                        <div class="form-group">
                            <div class="col-md-3 col-md-offset-3">
                                <g:submitButton class="btn btn-info" name="create" value="保存"/>
                            </div>
                        </div>
                </g:form>
            </div>
        </div>
    </div>
</div>
<g:javascript>
$(function() {
  function getCityByProvince() {
          $.ajax({
              type: "POST",
              url: "/province/getCityByProvince",
              data: {
                  province: $("#provinceId").val(),
              },
              success: function (data) {
                  var cityList = data.cityList;
                  var option;
                  if (data.status == "success") {
                      $('.cityName').html("");
                      $(".cityName").append('<option value="">请选择</option>')
                      for (var i = 0; i < cityList.length; i++) {
                          option = '<option value="' + cityList[i].id + '"';
                          if ($("#cityId").val() == cityList[i].id)
                          {
                            option += ' selected="selected" '
                          }
                          option += '>' + cityList[i].name + '</option>'
                          $(".cityName").append(option)
                      }
                      $("select").select2();
                  }
              },
              error: function () {
                console.log("获取失败，请稍后重试");
                  // swal("获取失败，请稍后重试", "", "error");
              }
          });
      }

      getCityByProvince();

      $("#province").change(function () {
            $("#provinceId").val($("#province option:selected").val())
            getCityByProvince();
        });
})
</g:javascript>

</body>

</html>
